<?php




namespace App\Http\Controllers\Api\Site;


use App\Http\Controllers\Api\BasicController;
use App\Models\SiteCode;
use App\Repositories\DataStatus;
use App\Repositories\ResponseCode;
use App\Repositories\ResponseMsg;
use App\Services\Sms;
use Jiannei\Response\Laravel\Support\Facades\Response;

class CodeController extends BasicController
{
    /**
     * @api {post} /site/code/sms 发送短信
     * @apiName SiteCodeSms
     * @apiGroup SiteCode
     *
     * @apiParam {number} mobile 手机号
     * @apiParam {number} type 类型 1=注册2=登录3=修改密码4=变更资料5=其他
     *
     */

    public function sms()
    {
        $data = $this->data(['mobile', 'type']);
        return (new Sms())->send($data['mobile'], $data['type']);
    }

    /**
     * @return bool
     * @api {post} /site/code/check 验证码验证
     * @apiName SiteCodeCheck
     * @apiGroup SiteCode
     *
     * @apiParam {String} mobile/email 手机号/邮箱
     * @apiParam {number} code 验证码
     * @apiParam {number} type 类型 1=注册2=登录3=修改密码4=变更资料5=其他
     */

    public function check()
    {
        $data = $this->data(['email', 'mobile', 'code', 'type']);
        $to = $data['email'] ?? $data['mobile'];
        $result = (new SiteCode())->checkCode($to, $data['code'], $data['type']);
        if ($result) {
            $result->status = DataStatus::DATA_NO;
            $result->save();
            Response::ok();
        } else {
            Response::fail(ResponseMsg::SMS_CODE_INVALID, ResponseCode::SMS_CODE_INVALID);
        }
    }
}
